package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MSetmealDao;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MSetmealService.class)
@Transactional
public class MSetmealServiceImpl implements MSetmealService {
    @Autowired
    private MSetmealDao mSetmealDao;
    @Override
    public Result findAll() {
        List<Setmeal> all = mSetmealDao.findAll();
        return new Result(true, "查询成功",all);
    }

    @Override
    public Result findById(Integer id) {
        Setmeal daoById = mSetmealDao.findById(id);
        List<CheckGroup> checkGroupById = mSetmealDao.findGroupById(id);
        List<CheckItem> checkItems = new ArrayList<>();
        try {
            for (CheckGroup group : checkGroupById) {
//               //查出来与groupid对应的List<CheckItem>
                checkItems = mSetmealDao.findItemsById(group.getId());
                group.setCheckItems(checkItems);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        daoById.setCheckGroups(checkGroupById);
        return new Result(true, "查询成功",daoById);
    }
}